//
//  refraction.metal
//  ARDEMO_1
//
//  Created by 37Design on 2021/5/10.
//

#include <metal_stdlib>
using namespace metal;

typedef struct {
vector_float4 position;
vector_float4 normal;
} MBEVertex;

float4 zfs(float zsl,float3 surfaceNormal,float3 surfaceView,texturecube<float> cubeTexture,sampler cubeSampler)
{
    float4 finalColorZS=float4(1.0,1.0,1.0,0.0);
    float4 finalColorFS=float4(1.0,1.0,1.0,0.0);
    float4 finalColor=float4(1.0,1.0,1.0,0.0);
    float3 vTextureCoord=float3(1.0,1.0,1.0);
    //texturecube<float> cubeTexture;
    const float maxH=0.7;
    const float minH=0.2;
    float sizeH=maxH-minH;
    float3 eyeDirection=normalize(-surfaceView);
    float testValue=abs(dot(eyeDirection,surfaceNormal));
    if(testValue>maxH)
    {
        vTextureCoord=refract(-eyeDirection,surfaceNormal,zsl);
        finalColor=cubeTexture.sample(cubeSampler,vTextureCoord);
    }else if(testValue<=maxH&&testValue>=minH)
    {
        vTextureCoord=reflect(-eyeDirection,surfaceNormal);
        finalColorFS=cubeTexture.sample(cubeSampler,vTextureCoord);
        vTextureCoord=refract(-eyeDirection,surfaceNormal,zsl);
        finalColorZS=cubeTexture.sample(cubeSampler,vTextureCoord);
        float ratio=(testValue-minH)/sizeH;
        finalColor=finalColorZS*ratio+(1.0-ratio)*finalColorFS;
        
    }else
    {
        vTextureCoord=reflect(-eyeDirection,surfaceNormal);
        finalColorFS=cubeTexture.sample(cubeSampler,vTextureCoord);
    }
   
    return finalColor;
    
}

#pragma clang diagnostic ignored "-Wmissing-prototypes"
#pragma clang diagnostic ignored "-Wmissing-braces"

#include <metal_stdlib>
#include <simd/simd.h>

using namespace metal;

template<typename T, size_t Num>
struct spvUnsafeArray
{
    T elements[Num ? Num : 1];
    
    thread T& operator [] (size_t pos) thread
    {
        return elements[pos];
    }
    constexpr const thread T& operator [] (size_t pos) const thread
    {
        return elements[pos];
    }
    
    device T& operator [] (size_t pos) device
    {
        return elements[pos];
    }
    constexpr const device T& operator [] (size_t pos) const device
    {
        return elements[pos];
    }
    
    constexpr const constant T& operator [] (size_t pos) const constant
    {
        return elements[pos];
    }
    
    threadgroup T& operator [] (size_t pos) threadgroup
    {
        return elements[pos];
    }
    constexpr const threadgroup T& operator [] (size_t pos) const threadgroup
    {
        return elements[pos];
    }
};

struct type_View
{
    char _m0_pad[1120];
    float3 View_PreViewTranslation;
    char _m1_pad[928];
    float4 View_ViewRectMin;
    float4 View_ViewSizeAndInvSize;
    char _m3_pad[68];
    float View_PreExposure;
    char _m4_pad[336];
    float3 View_IndirectLightingColorScale;
    char _m5_pad[464];
    float4 View_SkyLightColor;
    char _m6_pad[120];
    float View_ReflectionCubemapMaxMip;
    char _m7_pad[16];
    float3 View_ReflectionEnvironmentRoughnessMixingScaleBiasAndLargestWeight;
};

struct type_MobileBasePass
{
    int MobileBasePass_UseCSM;
};

struct type_MobileDirectionalLight
{
    float4 MobileDirectionalLight_DirectionalLightColor;
    float4 MobileDirectionalLight_DirectionalLightDirectionAndShadowTransition;
    float4 MobileDirectionalLight_DirectionalLightShadowSize;
    float4 MobileDirectionalLight_DirectionalLightDistanceFadeMADAndSpecularScale;
    float4 MobileDirectionalLight_DirectionalLightShadowDistances;
    spvUnsafeArray<float4x4, 4> MobileDirectionalLight_DirectionalLig;
};



